﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class ProdCateBLL
  {
    //Get all

    public List<ProdCate> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {        
        return data.ProdCates.OrderBy(p => p.Sort).ToList();
      }
    }

    public List<ProdCate> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.ProdCates.Where(p => p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get By Parent
    public List<ProdCate> GetByParent(int idParent)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.ProdCates.Where(p => p.Parent == idParent).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<ProdCate> GetByParent(int idParent, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.ProdCates.Where(p => p.Parent == idParent && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get by Level
    public List<ProdCate> GetByLevel(int level)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.ProdCates.Where(p => p.Level == level).OrderBy(p => p.Sort).ToList();
      }
    }

    public List<ProdCate> GetByLevel(int level, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.ProdCates.Where(p => p.Level == level && p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get by Page
    public List<ProdCate> GetByPage(List<ProdCate> list, int page, int size)
    {
      page--;
      int skip = page * size;
      return list.Skip(skip).Take(size).ToList();
    }

    //Get by id
    public ProdCate GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.ProdCates.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public ProdCate GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.ProdCates.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Get max sort
    public byte GetMaxSort(int idParent)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        ProdCate obj = data.ProdCates.Where(p => p.Parent == idParent).OrderByDescending(p => p.Sort).FirstOrDefault();
        if (obj != null)
          return (byte)(obj.Sort + 1);
        else
          return 1;
      }
    }

    //Swap sort
    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        ProdCate objFirst = data.ProdCates.Where(p => p.Id == idFirst).FirstOrDefault();
        ProdCate objSecond = data.ProdCates.Where(p => p.Id == idSecond).FirstOrDefault();

        byte tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }

    public bool IsHasChild(int id)
    {
      if (GetByParent(id).Count == 0)
        return false;
      else return true;
    }

    //Enable
    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        ProdCate obj = data.ProdCates.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }
   
    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        ProdCate obj = data.ProdCates.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Add
    public void Add(ProdCate obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.ProdCates.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update
    public void Update(ProdCate obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        ProdCate prodCate = data.ProdCates.Where(p => p.Id == obj.Id).FirstOrDefault();

        prodCate.Image = obj.Image;
        prodCate.isEnable = obj.isEnable;
        prodCate.Level = obj.Level;
        prodCate.Name = obj.Name;
        prodCate.Name_En = obj.Name_En;
        prodCate.Parent = obj.Parent;
        prodCate.Sort = obj.Sort;
        prodCate.CreatedDate = obj.CreatedDate;
        data.SubmitChanges();
      }
    }

    //Delete
    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        ProdCate obj = data.ProdCates.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
        {
          data.ProdCates.DeleteOnSubmit(obj);
          data.SubmitChanges();
        }
      }
    }
  }
}
